package com.probiz.estore.exception;

import com.probiz.estore.common.model.product.ProductSku;
import com.probiz.estore.common.model.sales.Gift;
import com.probiz.estore.core.util.I18nUtil;
import com.probiz.estore.webapp.util.RequestContext;

/**
 * 库存不足异常
 * @since V3.0
 */
public class OutOfStockException extends Exception{
	
	private static final long	serialVersionUID	= 6445963130649662622L;
	
	private Integer canAllocateAvailableQty;
	private ProductSku productSku;
	private Gift gift;

	
	public OutOfStockException(ProductSku productSku, Integer canAllocateAvailableQty) {
		super();
		this.productSku = productSku;
		this.canAllocateAvailableQty = canAllocateAvailableQty;
	}

	public OutOfStockException(Gift gift, Integer canAllocateAvailableQty) {
		super();
		this.gift = gift;
		this.canAllocateAvailableQty = canAllocateAvailableQty;
	}

	public Integer getCanAllocateAvailableQty() {
		return canAllocateAvailableQty;
	}
	public void setProductSku(ProductSku productSku) {
		this.productSku = productSku;
	}
	
	public ProductSku getProductSku() {
		return productSku;
	}

	public void setGift(Gift gift) {
		this.gift = gift;
	}

	@Override
	public String getMessage() {
		StringBuilder sb = new StringBuilder();
		if (this.productSku != null) {
			if(RequestContext.isFront()){
				if (getCanAllocateAvailableQty() == 0) {
					sb.append(I18nUtil.getInstance().getMessage("shoppingcart.message.stockLack",new Object[]{productSku.getProduct().getProductName()}));
				} else {
					sb.append(I18nUtil.getInstance().getMessage("shoppingcart.message.stockNotEnough",new Object[]{productSku.getProduct().getProductName(),canAllocateAvailableQty}));
				}
			}else{
				if (getCanAllocateAvailableQty() == 0) {
					sb.append(I18nUtil.getInstance().getMessage("shoppingcart.message.stockLack4Admin",new Object[]{productSku.getProduct().getProductName()}));
				} else {
					sb.append(I18nUtil.getInstance().getMessage("shoppingcart.message.stockNotEnough4Admin",new Object[]{productSku.getProduct().getProductName(),canAllocateAvailableQty}));
				}
			}
		} else {
			if(RequestContext.isFront()){
				if (getCanAllocateAvailableQty() == 0) {
					sb.append(I18nUtil.getInstance().getMessage("shoppingcart.message.stockLack",new Object[]{gift.getGiftName()}));
				} else {
					sb.append(I18nUtil.getInstance().getMessage("shoppingcart.message.stockNotEnough",new Object[]{gift.getGiftName(),canAllocateAvailableQty}));
				}
			}else{
				if (getCanAllocateAvailableQty() == 0) {
					sb.append(I18nUtil.getInstance().getMessage("shoppingcart.message.stockLack4Admin",new Object[]{gift.getGiftName()}));
				} else {
					sb.append(I18nUtil.getInstance().getMessage("shoppingcart.message.stockNotEnough4Admin",new Object[]{gift.getGiftName(),canAllocateAvailableQty}));
				}
			}
		}
		
		return sb.toString();
	}

}
